//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __ELA_MISC_H__
#define __ELA_MISC_H__

#include "ddk.h"
/*===========================================================================
//
//      codes.h
//
//      Common error code definitions
//
//===========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//===========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    jlarmour
// Contributors: jlarmour
// Date:         2000-04-14
// Purpose:      To provide a common set of error codes
// Description:  This provides a common set of error codes that all
//               packages can agree on. It doesn't preclude them defining
//               their own error return system, but this is a preferable
//               system to use to help error support be as general as
//               possible.
//
//               We try and conform to the ANSI/POSIX error code format,
//               namely starting with the character 'E'
//
// Usage:        #include <cyg/error/codes.h>
//
//               Example:
//              
//               err=myfun();
//               if (err != ENOERR)
//               {
//                 str=strerror(err);
//                 printf("myfun returned error: %s\n", str);
//               }
//               else ....
//
//####DESCRIPTIONEND####
//
//=========================================================================*/

/* CONFIGURATION */

#ifdef __cplusplus
extern "C"
{
#endif

/* TYPE DEFINITIONS */

/* A type for error codes which may be useful to explain the purpose of
     * a variable or return code. It shows that it contains an error code
     * of the type defined below */

/* CONSTANT DEFINITIONS */

/* If adding to this list, you must also update strerror() with its text
     * If there is a common error of the same purpose on Unix, try and use its
     * name and number. If not, use one above 200 to prevent future conflicts
     *
     * Do not use negative numbers, so that functions can return positive on
     * success and ESOMETHING on error, and it all works consistently.
    */
#define ENOERR           (0)     /* No error */
#define EPERM            (-1)     /* Not permitted */
#define ESRCH            (-3)     /* No such process */
#define EINTR            (-4)     /* Operation interrupted */
#define EIO              (-5)     /* I/O error */
#define EAGAIN           (-11)    /* Try again later */
#define ENOMEM           (-12)    /* Out of memory */
#define EBUSY            (-16)    /* Resource busy */
#define EEXIST           (-17)   /* File exists */
#define ENOTDIR          (-20)    /* Not a directory */
#define EISDIR           (-21)    /* Is a directory */
#define EINVAL           (-22)    /* Invalid argument */
#define ENOSPC           (-28)    /* No space left on device */
#define EROFS            (-30)    /* Read-only file system */
#define ENOROOT          (-33)    /* No root had been mounted */
                               /* domain */

//#define JFFS2_DEBUGINFO

#define abs(x) ((x)>0?(x):(-x))

inline ECode ErrorToECODE(Int32 errno, const char* fn)
{
    switch(errno) {
        case 0:
            return NOERROR;

        case EINVAL:
            return E_INVALID_ARGUMENT;

        case ENOMEM:
            return E_OUT_OF_MEMORY;

        default:
            assert(errno <= EPERM && errno >= ENOROOT);
            int nError = abs(errno);
            ECode ec = FILESYS_ERROR(nError);
#ifdef JFFS2_DEBUGINFO
            kprintf(("JFFS2: Error in %s, errnor = %d, ec = 0x%08x\n", fn, nError, ec));
#endif
            return ec;
    }
}

#ifdef __cplusplus
}   /* extern "C" */
#endif

#endif /* __ELA_MISC_H__ multiple inclusion protection */

/* EOF misc.h */
